package com.yixun.lbx.service.impl;

import com.yixun.lbx.controller.WebSocket;
import com.yixun.lbx.entity.YxProLegalInfo;
import com.yixun.lbx.mapper.information.LegalInfoMapper;
import com.yixun.lbx.service.LbxRedisService;
import com.yixun.lbx.service.LegalInfoServie;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class LegalInfoServieImpl implements LegalInfoServie {

    private final LegalInfoMapper legalInfoMapper;
    private final LbxRedisService redisService;

    public LegalInfoServieImpl(LegalInfoMapper legalInfoMapper, LbxRedisService redisService) {
        this.legalInfoMapper = legalInfoMapper;
        this.redisService = redisService;
    }

    @Override
    public Integer getCount(HashMap<String, Object> map) {
        return legalInfoMapper.getCount(map);
    }

    @Override
    public List<YxProLegalInfo> getListByPage(HashMap<String, Object> map) {
        return legalInfoMapper.getListByPage(map);
    }

    @Override
    public int addLegalInfo(HashMap<String, Object> map) {
        map.put("addTime", getDate());
        Integer isPublic = (Integer) map.get("isPublic");
        int i = legalInfoMapper.insertSelective(map);


        if (isPublic == 0) {
            String releasedDtime = (String) map.get("releasedDtime");
            if ((releasedDtime).length() < 19) {
                releasedDtime = releasedDtime + ":00";
            }
            redisService.setKeyValue("timer:" + map.get("id"), releasedDtime);
            this.timer((String) map.get("id"));
        }
        return i;
    }

    @Override
    public void timer(String id) {
        String dtime = redisService.getKey("timer:" + id);
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dtime.replace("T", " "));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        assert parse != null;
        long parseTime = parse.getTime();
        long nowTime = new Date().getTime();
        long l = parseTime - nowTime;
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        Runnable runnable = new Runnable() {
            public void run() {
                String dtime2 = redisService.getKey("timer:" + id);
                if (dtime2.equals(dtime)) {
                    try {
                        int i = legalInfoMapper.updateIsPublic(id);
                        redisService.deleteKey("timer:" + id);
                        new WebSocket().onMessage(legalInfoMapper.selectByPrimaryKey(Long.valueOf(id)).getTitle(),null);
                        if (i == 0) {
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                }
            }
        };
        service.schedule(runnable, l, TimeUnit.MILLISECONDS);
        service.shutdown();
    }

    @Override
    public List<YxProLegalInfo> farm_getListByPage(HashMap<String, Object> map) {
        return legalInfoMapper.farm_getListByPage(map);
    }

    @Override
    public List<YxProLegalInfo> getShowList() {
        return legalInfoMapper.getShowList();
    }

    @Override
    public int updateLegalInfo(HashMap<String, Object> map) {
        map.put("updateTime", getDate());
        Integer isPublic = (Integer) map.get("isPublic");
        int i = legalInfoMapper.updateByPrimaryKeySelective(map);
        if (isPublic == 0) {
            String releasedDtime = (String) map.get("releasedDtime");
            if (releasedDtime.length() < 19) {
                releasedDtime = releasedDtime + ":00";
            }
            redisService.setKeyValue("timer:" + map.get("id"), releasedDtime);
            this.timer((String) map.get("id"));
        } else {
            redisService.deleteKey("timer:" + map.get("id"));
        }
        return i;
    }


    @Override
    public int deleteLegalInfo(HashMap<String, Object> map) {
        redisService.deleteKey("timer:" + map.get("id"));
        return legalInfoMapper.deleteByPrimaryKey(map);
    }

    @Override
    public List<YxProLegalInfo> getObjByArgs(YxProLegalInfo legalInfo) {
        return legalInfoMapper.getObjByArgs(legalInfo);
    }

    @Override
    public List<YxProLegalInfo> getSearchListByPage(HashMap<String, String> map) {
        return legalInfoMapper.getSearchListByPage(map);
    }

    private String getDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
}
